package com.zingrow.web.configure.finaldb;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.ExcelUtils;
import com.zingrow.common.utils.MongoHelper;
import com.zingrow.common.utils.OptClass;
import com.zingrow.common.utils.PageView;
import com.zingrow.common.utils.WordGenerator;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.Menu;
import com.zingrow.web.base.enums.Operation;
import com.zingrow.web.support.operate.model.OptRecord;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.thriftservice.Articles;
import com.zingrow.web.thriftservice.ElasticSearchService;
import com.zingrow.web.user.model.ActiveUser;
import com.zingrow.web.user.model.UserPermission;
import com.zingrow.web.user.service.IPermissionService;
@Controller
@RequestMapping(value="/FinalDb/",produces="application/json;charset=UTF-8")
public class FinalDbController extends BaseController{
	@Autowired
	private IOptRecordService optRecordService;
	@Autowired
	private IPermissionService permissionService;
	//连接服务地址
	private static String persist_service_ip = null;
	@Autowired
	private IOptRecordService ioptRecordService;
	
	private void init() {
		try {
			if(StringUtils.isBlank(persist_service_ip)) {
				FileInputStream fis = new FileInputStream(new File(
						MongoHelper.class.getResource("/").getPath(),
						"properties/activemq.properties"));
				Properties properties = new Properties();
				properties.load(fis);
				persist_service_ip = properties.getProperty("persist_service_ip");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * //多条件模糊查询
	 * @param request
	 * @param response
	 * @param articles
	 * @param pageSize
	 * @param pageIndex
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryList")
	private String queryList(HttpServletRequest request,HttpServletResponse response,Articles articles,Integer pageSize,Integer pageIndex,String sp) {
		init();
		TTransport transport = null;
		ReturnStandardDataFormat dataFormat = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		List<Articles> list = new ArrayList<Articles>();
		//总页数
		long number = 0;
		//总条数
		long count = 0;
		PageView pageView = null;
		try {
			//连接地址
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			//分页初始值
			if(pageIndex == null || pageIndex <= 0){
				pageIndex = 1;
			}
			if(pageSize == null || pageSize <= 0){
				pageSize = 10;
			}
			//中文转码
			if(articles.getTitle()!=null || "".equals(articles.getTitle())) {
				articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
			}
			if(articles.getContent()!=null || "".equals(articles.getContent())) {
				articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
			}
			if(articles.getSource()!=null || "".equals(articles.getSource())) {
				articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
			}
			if(articles.getSeedType()!=null || "".equals(articles.getSeedType())) {
				articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
			}
			pageView = new PageView(pageSize,pageIndex);
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//调用分页多条件查询
			list = client.queryList(pageView.getPageNow(),pageView.getPageSize(), articles);
			//判断list集合是否有值
			if(list.size()>0) {
				//得到数据总数
				count = list.get(0).getArticleCount();
				for (Articles cles : list) {
					if(cles.getSeedType().equals("Micro")) {
						cles.setSource(cles.getBlogerName());
						
						//去掉内容中的中英文空格、标签、和空格换行等
						cles.setMicroContent(cles.getMicroContent().replaceAll("　| |　", "").replaceAll("</?[^>]+>","").replaceAll("\\&[a-zA-Z]{1,10};", ""));
						cles.setFcontent(cles.getFcontent().replaceAll("　| |　", "").replaceAll("</?[^>]+>","").replaceAll("\\&[a-zA-Z]{1,10};", ""));
						//转译
						cles.setContent(cles.getMicroContent()+cles.getFcontent());
					} else {
						//去掉内容中的中英文空格、标签、和空格换行等
						cles.setContent(cles.getContent().replaceAll("　| |　", "").replaceAll("</?[^>]+>","").replaceAll("\\&[a-zA-Z]{1,10};", ""));
						//html转译
						cles.setContent(HtmlUtils.htmlEscape(cles.getContent()));
						//定义一些特殊字符的正则表达式 如：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						cles.setContent(cles.getContent().replaceAll("\\&[a-zA-Z]{1,10};", ""));
					}
				}
				//计算有多少页
				number = count%pageView.getPageSize()==0 ? count/pageView.getPageSize() : count /pageView.getPageSize()+1;
			}
			//新增一条操作记录
			//操作日志
			if("11".equals(sp))
			{//判断是否为自主点击查询*/
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().final_query(opuser,Menu.informationmanagement.toString(),
					Menu.FinalDb.toString(),Operation.query.toString(),articles);
			if(s.getOptName()!="null")
			ioptRecordService.insert(s);
			}
		} catch (Exception e) {
			log.error("查询异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"总库信息查询异常");
			ioptRecordService.insert(s);
			dataFormat.setReturnCode(CodeMsg.CODE_EXCEPTION);
			dataFormat.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			//总页数set到dataFormat中
			dataFormat.setTotalPages(number);
			//总条数set到dataFormat中
			dataFormat.setTotalRecords(count);
			//数据集合set到dataFormat中
			dataFormat.setReturnData(list);
			return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
		} finally {
			//关闭连接
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
		//得到集合set到dataFormat中
		dataFormat.setReturnData(list);
		//把总页数set到dataFormat中
		dataFormat.setTotalPages(number);
		//把总条数set到dataFormat中
		dataFormat.setTotalRecords(count);
		dataFormat.setReturnCode(CodeMsg.CODE_OK);
		dataFormat.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(dataFormat, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * //删除
	 * @param request
	 * @param response
	 * @param id
	 * @param seedType
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest request,HttpServletResponse response,String id,String seedType,String title) {
		TTransport transport = null;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		try {
			//连接地址
			if (title != "" || "".equals(title)) {
				title = EncodingUtils.fixUrlParamEncoding(request, title);
			}
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			//判断参数是否合法
			if(CommonUtils.isObjectNull(id) && CommonUtils.isObjectNull(seedType)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			} else {
				//中文转码
				seedType = EncodingUtils.fixUrlParamEncoding(request, seedType);
			}
			if(seedType.equals("新闻")){
				seedType="News";
			} else if(seedType.equals("论坛")) {
				seedType="BBS";
			} else if(seedType.equals("微信")) {
				seedType="WeiXin";
			} else if(seedType.equals("微博")) {
				seedType="Micro";
			}
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			Articles skk=client.queryById(id);
			if(client.deleteData(id, seedType)==1000) {
				//新增一条操作记录
				if(title!=null&&!"".equals(title))
				{
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().final_delete(opuser,Menu.informationmanagement.toString(),
						Menu.FinalDb.toString(),Operation.delete.toString(),title,"1");
				if(s.getOptName()!="null")
				ioptRecordService.insert(s);	
				}
				else
				{
					ActiveUser opuser = getUser(request);//获取操作用户
					OptRecord s = new OptClass().final_delete(opuser,Menu.informationmanagement.toString(),
							Menu.FinalDb.toString(),Operation.delete.toString(),skk.getUrl(),"2");
					if(s.getOptName()!="null")
					ioptRecordService.insert(s);		
				}
				//操作成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				//操作失败
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (Exception e) {
			log.error("删除异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"删除总库信息异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		} finally {
			//关闭连接
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
	}
	
	/**
	 * //新增
	 * @param request
	 * @param response
	 * @param articles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insert")
	public String insert(HttpServletRequest request,HttpServletResponse response,Articles articles) {
		TTransport transport = null;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		try {
			//连接地址
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			//判断新增类型
			if(articles.seedType.equals("WeiXin")) {
				if(CommonUtils.isObjectNull(articles.getTitle())||CommonUtils.isObjectNull(articles.getSource())
				||CommonUtils.isObjectNull(articles.getSeedType())||CommonUtils.isObjectNull(articles.getUrl())
				||CommonUtils.isObjectNull(articles.getContent())||CommonUtils.isObjectNull(articles.getAuthor())){
					standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				} else {
					//中文转码
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
					articles.setAuthor(EncodingUtils.fixUrlParamEncoding(request, articles.getAuthor()));
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
			} else {
				//判断参数是否正确
				if(CommonUtils.isObjectNull(articles.getTitle())||CommonUtils.isObjectNull(articles.getSource())
				||CommonUtils.isObjectNull(articles.getSeedType())||CommonUtils.isObjectNull(articles.getUrl())
				||CommonUtils.isObjectNull(articles.getContent())){
					standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				} else {
					//中文转码
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
					articles.setAuthor(EncodingUtils.fixUrlParamEncoding(request, articles.getAuthor()));
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
			}
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//调用新增
			if(client.insertData(articles)==1000) {
				//新增一条操作记录
				//操作日志
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().final_add(articles,opuser,Menu.informationmanagement.toString(),
						Menu.FinalDb.toString(),Operation.insert.toString());
				if(s.getOptName()!="null")
				ioptRecordService.insert(s);		
				//新增成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				//新增失败
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().recordabnormality(opuser,"新增总库信息异常");
				ioptRecordService.insert(s);
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
			}
		} catch (Exception e) {
			log.error("新增异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"新增总库信息异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
		} finally {
			//关闭连接
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
	}
	
	/**
	 * //修改
	 * @param request
	 * @param response
	 * @param articles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(HttpServletRequest request,HttpServletResponse response,Articles articles) {
		TTransport transport = null;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		try {
			//连接地址
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			//判断类型
			if(articles.getSeedType().equals("WeiXin")) {
				//判断参数是否正确
				if(CommonUtils.isObjectNull(articles.getTitle())||CommonUtils.isObjectNull(articles.getSource())
				||CommonUtils.isObjectNull(articles.getSeedType())||CommonUtils.isObjectNull(articles.getUrl())
				||CommonUtils.isObjectNull(articles.getContent())||CommonUtils.isObjectNull(articles.getAuthor())){
					
					standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				} else {
					//中文转码
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
					articles.setAuthor(EncodingUtils.fixUrlParamEncoding(request, articles.getAuthor()));
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
			} else {
				//判断参数是否正确
				if(CommonUtils.isObjectNull(articles.getTitle())||CommonUtils.isObjectNull(articles.getSource())
				||CommonUtils.isObjectNull(articles.getSeedType())||CommonUtils.isObjectNull(articles.getUrl())
				||CommonUtils.isObjectNull(articles.getContent())) {
					
					standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				} else {
					//中文转码
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
					articles.setAuthor(EncodingUtils.fixUrlParamEncoding(request, articles.getAuthor()));
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
			}
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//调用修改方法
			if(client.updateData(articles)==1000) {
				//新增一条操作记录
				//操作日志
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().final_update(opuser,Menu.informationmanagement.toString(),
						Menu.FinalDb.toString(),Operation.update.toString(),articles.getTitle(),0);
				ioptRecordService.insert(s);	
				//修改成功
				standardData.setReturnCode(CodeMsg.CODE_OK);
				standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				//修改失败
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().recordabnormality(opuser,"总库信息修改异常");
				ioptRecordService.insert(s);
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
			}
		} catch (Exception e) {
			log.error("修改异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"总库信息修改异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
		} finally {
			//关闭连接
			if (null != transport && transport.isOpen()) {
				transport.close();
			}
		}
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");	
	}
	
	/**
	 * //单个查询
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("findId")
	public String findId(HttpServletRequest request,HttpServletResponse response,String id) {
		TTransport transport = null;
		Articles articles = new Articles();
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		try {
			//连接地址
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			//判断参数是否正确
			if (CommonUtils.isObjectNull(id)) {
				standardData.setReturnCode(CodeMsg.CODE_ERROR_PARAMS);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR_PARAMS);
			}
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//调用方法
			articles = client.queryById(id);
			//判断得到的对象是否为空
			if(articles!=null) {
				//判断类型
				if(articles.getSeedType().equals("Micro")) {
					articles.setSource(articles.getBlogerName());
					if(articles.getMicroContent()!=null && !articles.getMicroContent().equals("")) {
						articles.setSource(articles.getBlogerName());
						articles.setContent(articles.getMicroContent());
					}
					//添加微博查看操作日志
					//新增一条操作记录
					//操作日志
					ActiveUser opuser = getUser(request);//获取操作用户
					OptRecord s = new OptClass().final_update(opuser,Menu.informationmanagement.toString(),
							Menu.FinalDb.toString(),Operation.querys.toString(),articles.getUrl(),1);
					ioptRecordService.insert(s);	
				} 
					//操作成功
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage(CodeMsg.MSG_OK);
			} else {
				//操作失败
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (Exception e) {
			log.error("查询异常");
			ActiveUser opuser = getUser(request);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"总库信息单个查询异常");
			ioptRecordService.insert(s);
			standardData.setReturnData(articles);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		} finally {
			//关闭连接
			if(transport != null && transport.isOpen()) {
				transport.close();
			}
		}
			standardData.setReturnData(articles);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * //导出列表
	 * @param request
	 * @param response
	 * @param listids
	 * @param articles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("exportExcelForPageData")
	public String exportExcelForPageData(HttpServletRequest request,HttpServletResponse response,String listids,Articles articles) {
		TTransport transport = null;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		try {
			transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
			TProtocol protocol = new TCompactProtocol(transport);
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//定义表头
			List<String> titles = Arrays.asList("信息类型","发布时间","标题","首发媒体","站点","作者","正文统计数",
					"评级(研报)","阅读量(微信,论坛)","点赞量(微信)","回复数(论坛)","文章链接","标签");
			//定义表的字段名
			List<String> paramsName = Arrays.asList("seedType","publishTime","title","source","domainName","author","contentLength",
					"rate","viewCount","likeCount","replyCount","url","websiteSign");
			//传入IDS
			List<String> ids = new ArrayList<String>();
			List<Articles> clesList = new ArrayList<Articles>();
			//判断listids是否为空
			if(StringUtils.isNoneBlank(listids)) {
				for (String idslist : listids.split(",")) {
					ids.add(idslist);
				}
				//调用根据id查询数据
				clesList = client.queryByIds(ids);
			} else {
				//中文转码
				if(articles.getTitle()!=null || "".equals(articles.getTitle())) {
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
				}
				if(articles.getContent()!=null || "".equals(articles.getContent())) {
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
				if(articles.getSource()!=null || "".equals(articles.getSource())) {
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
				}
				if(articles.getSeedType()!=null || "".equals(articles.getSeedType())) {
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
				}
				//调用根据多条件查询，导出所有数据
				clesList = client.queryList(0, 0, articles);
			}
			//定义一个输出流
			OutputStream out = null;
			try {
				//获取输出流
				out = response.getOutputStream();
				//清空流
				response.reset();
				//定义响应输出类型
				response.setContentType("application/msexcel");
				//设置输出文件头
				response.setHeader("Content-disposition",
                        "attachment; filename=" + new String("总库信息列表".getBytes("GBK"), "iso8859-1") + ".xls");
				ExcelUtils.exportExcel(titles, paramsName, clesList, out);
				//操作日志
				ActiveUser opuser = getUser(request);//获取操作用户
				OptRecord s = new OptClass().goiss(opuser,Menu.informationmanagement.toString(),
						Menu.FinalDb.toString(),Operation.Theexportlist.toString(),ids.size());
				ioptRecordService.insert(s);
			} catch (Exception e) {
				log.error(e.getMessage());
			} finally {
				//关闭流
				out.close();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			//关闭连接
			if(null!=transport && transport.isOpen()) {
				transport.close();
			}
		}
		
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * //导出文章
	 * @param request
	 * @param response
	 * @param ids
	 * @param articles
	 * @return
	 */
	@ResponseBody
	@RequestMapping("exportWordForPageData")
	public String exportWordForPageData(HttpServletRequest request,HttpServletResponse response,String ids,Articles articles) {
		TTransport transport = null;
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR,CodeMsg.MSG_ERROR,null);
		List<Articles> cleslist = new ArrayList<Articles>();
		List<String> listIds = new ArrayList<String>();
		List<Map<String,Object>> list = new ArrayList<>();
		transport = new TFramedTransport(new TSocket(persist_service_ip, 10086));
		TProtocol protocol = new TCompactProtocol(transport);
		try {
			TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "ElasticSearchService");
			ElasticSearchService.Client client = new ElasticSearchService.Client(multiplexedProtocol);
			//打开连接
			transport.open();
			//判断IDS是否有值
			if (StringUtils.isNotBlank(ids)) {
				for (String idslist : ids.split(",")) {
					listIds.add(idslist);
				}
				//调用根据id查询
				cleslist = client.queryByIds(listIds);
				for (Articles cles : cleslist) {
					if(cles.seedType.equals("Micro")) {
						if(cles.getTitle()==null || cles.getTitle().equals("")) {
							cles.setTitle("微博");
						}
						if(cles.getSource()==null || cles.getSource().equals("")) {
							cles.setSource("无");
						}
						if(cles.getAuthor()==null || cles.getAuthor().equals("")) {
							cles.setAuthor("无");
						}
						if(cles.getContent()==null || cles.getContent().equals("")) {
							cles.setContent("无");
						} else {
							//去掉内容中的中英文空格、标签、空格和换行等
							cles.setContent(cles.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>","").replaceAll("&nbsp;", "").replaceAll("\\s*|\t|\r|\n", ""));
						}
					} else {
						if(cles.getTitle()==null || cles.getTitle().equals("")) {
							cles.setTitle("无");
						} else if(cles.getSource()==null || cles.getSource().equals("")) {
							cles.setSource("无");
						} else if(cles.getAuthor()==null || cles.getAuthor().equals("")) {
							cles.setAuthor("无");
						}
						if(cles.getContent()==null || cles.getContent().equals("")) {
							cles.setContent("无");
						} else {
							//去掉内容中的中英文空格、标签、空格和换行等
							cles.setContent(cles.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>","").replaceAll("&nbsp;", "").replaceAll("\\s*|\t|\r|\n", ""));
						}
					}
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("title", cles.getTitle());
					map.put("source", cles.getSource());
					map.put("author", cles.getAuthor());
					map.put("content", cles.getContent());
					list.add(map);
				}
			} else {
				//中文转码
				if(articles.getTitle()!=null || "".equals(articles.getTitle())) {
					articles.setTitle(EncodingUtils.fixUrlParamEncoding(request, articles.getTitle()));
				}
				if(articles.getContent()!=null || "".equals(articles.getContent())) {
					articles.setContent(EncodingUtils.fixUrlParamEncoding(request, articles.getContent()));
				}
				if(articles.getSource()!=null || "".equals(articles.getSource())) {
					articles.setSource(EncodingUtils.fixUrlParamEncoding(request, articles.getSource()));
				}
				if(articles.getSeedType()!=null || "".equals(articles.getSeedType())) {
					articles.setSeedType(EncodingUtils.fixUrlParamEncoding(request, articles.getSeedType()));
				}
				//调用多条件查询，导出查询的所有数据
				cleslist = client.queryList(0, 0, articles);
				for (Articles cles : cleslist) {
					if(cles.seedType.equals("Micro")) {
						if(cles.getTitle()==null || cles.getTitle().equals("")) {
							cles.setTitle("微博");
						}
						
						if(cles.getSource()==null || cles.getSource().equals("")) {
							cles.setSource("无");
						}
						
						if(cles.getAuthor()==null || cles.getAuthor().equals("")) {
							cles.setAuthor("无");
						}
						
						if(cles.getContent()==null || cles.getContent().equals("")) {
							cles.setContent("无");
						} else {
							//去掉内容中的中英文空格、标签、空格和换行等
							cles.setContent(cles.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>","").replaceAll("&nbsp;", "").replaceAll("\\s*|\t|\r|\n", ""));
						}
					} else {
						if(cles.getTitle()==null || cles.getTitle().equals("")) {
							cles.setTitle("无");
						} else if(cles.getSource()==null || cles.getSource().equals("")) {
							cles.setSource("无");
						} else if(cles.getAuthor()==null || cles.getAuthor().equals("")) {
							cles.setAuthor("无");
						}
						if(cles.getContent()==null || cles.getContent().equals("")) {
							cles.setContent("无");
						} else {
							//去掉内容中的中英文空格、标签、空格和换行等
							cles.setContent(cles.getContent().replaceAll("　| ", "").replaceAll("</?[^>]+>","").replaceAll("&nbsp;", "").replaceAll("\\s*|\t|\r|\n", ""));
						}
					}
					Map<String,Object> map = new HashMap<String, Object>();
					map.put("title", cles.getTitle());
					map.put("source", cles.getSource());
					map.put("author", cles.getAuthor());
					map.put("content", cles.getContent());
					list.add(map);
				}
			}
			File file = null;
			InputStream fin = null;
			OutputStream out = null;
			try {
				//调用工具类WordGenerator的createDoc方法生成word文档
				Map<String,Object> root = new HashMap<>();
				root.put("blist", list);
				file = WordGenerator.createDocword(root, "resumeWord");
				fin = new FileInputStream(file);
				
				response.setCharacterEncoding("utf-8");
				response.setContentType("application/msword");
				//设置浏览器以下载方式处理该文件默认名为信息文章.doc
				response.addHeader("Content-Disposition", "attachment;filename="+new String("总库信息文章".getBytes("GBK"),"iso-8859-1") + ".doc");
					
				out = response.getOutputStream();
				byte[] buffer = new byte[512]; //缓存区
				int bytesToRead = -1;
				//通过循环将读入的word文件的内容输出到浏览器中
				while((bytesToRead = fin.read(buffer)) != -1){
					out.write(buffer,0,bytesToRead);
				}
			} catch (Exception e) {
				log.error(e.getMessage());
			}finally{
				if(fin != null) fin.close();
				if(out != null) out.close();//关闭流
				if(file != null) file.delete(); //删除临时文件
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			//关闭连接
			if(transport!=null && transport.isOpen()) {
				transport.close();
			}
		}
		//操作日志
		ActiveUser opuser = getUser(request);//获取操作用户
		OptRecord s = new OptClass().goiss(opuser,Menu.informationmanagement.toString(),
				Menu.FinalDb.toString(),Operation.Exportarticles.toString(),listIds.size());
		ioptRecordService.insert(s);	
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}
}
